package com.learn.order.controller;


import com.learn.order.domain.Order;
import com.learn.order.service.OrderService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class OrderController {


    @Resource
    private AmqpTemplate rabbitTemplate;
    @Resource
    private OrderService orderService;


    /**
     * 提交订单
     * @return
     */
    @GetMapping("/submit")
    public Integer submitOrder() {

        Order orderInfo = new Order();
        orderInfo.setStatus(0);
        orderService.save(orderInfo);

        rabbitTemplate.convertAndSend("ex.order", "order", orderInfo.getId());

        return orderInfo.getId();
    }

    /**
     * 支付
     * @return
     */
    @GetMapping("/pay/{id}")
    public Integer pay(@PathVariable Integer id) {

        Order orderInfo = orderService.getById(id);

        //订单过期
        if(orderInfo.getStatus().equals(2)){
            //跳转用户历史账单中查看因付款超时而取消的订单
            return 2;
        }else{
            orderInfo.setStatus(1);
            orderService.updateById(orderInfo);
            //跳转到付款成功页面
            return 1;

        }
    }

    @GetMapping("/orderList")
    public List<Order> orderList() {

        List<Order> orderInfos = orderService.list();

        return orderInfos;
    }
}
